# Clear memory
rm(list=ls()) # Clear environmental variables
gc() # memory garbage removal
used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
Ncells 4207633 224.8 7824414 417.9 NA 6840728 365.4
Vcells 7827112 59.8 14786712 112.9 16384 12513660 95.5
# set global options
knitr::opts_chunk$set(fig.width=12, fig.height=8, warning=FALSE, message=FALSE)
if(!require("pacman", quietly=T)) install.packages("pacman")
pacman::p_load(osfr,tinytex,tidyverse,keyring,knitr,readxl,data.table,colorspace, lpSolve,irr,here,lme4,broom,psych,zoo,units,ggdist,cowplot,ggcorrplot,dplyr, patchwork, cowplot, ggpmisc, forcats, pwr, ggpmisc, simstudy, explore, car,VIM,mice,flextable,here, remotes,magrittr,reshape2,descr,table1,tableone,gmodels,xtable,lavaan,corrplot,caret,corrgram,Hmisc,polycor,lavaan,kableExtra,DiagrammeR,grid,Gmisc,epitools)
# Get the path to the session's temporary directory
session_temp_dir <- tempdir()
# Define paths for the specific temporary subdirectories
raw_data_dir <- file.path(session_temp_dir, "raw_data")
processed_data_dir <- file.path(session_temp_dir, "processed_data")
# Create the 'raw_data' subdirectory if it doesn't exist
if (!dir.exists(raw_data_dir)) {
dir.create(raw_data_dir)
}
# Create the 'processed_data' subdirectory if it doesn't exist
if (!dir.exists(processed_data_dir)) {
dir.create(processed_data_dir)
}
osf_auth(keyring :: key_get("osf"))
Registered PAT from the provided token
# Define OSF node IDs at the beginning
# raw_data_node_id <- "yxsqb" # Master Thesis Raw Data Node or other Raw Data Node
# raw_data_node_id <- "gpj2v" # https://osf.io/gpj2v/ Quantify Occupational Injury
raw_data_node_id <- "dpgby" # https://osf.io/dpgby/ Art Business Education
# raw_data_node_id <- "z6jfn" # https://osf.io/z6jfn/ Housing prices
# raw_data_node_id <- "gu7ny" # https://osf.io/gu7ny/ Hmm53a-2425-GR1
# raw_data_node_id <- "prw4a" # https://osf.io/prw4a/ Hmm53a-2425-GR2
# raw_data_node_id <- "gxw3v" # https://osf.io/gxw3v/ Hmm53a-2425-GR3
# processed_data_node_id <- "e4rdh" # Master Thesis Processed Data Node
# processed_data_node_id <- "q4wgm" # https://osf.io/q4wgm/ Quantify Occupational Injury
processed_data_node_id <- "akgf2" # https://osf.io/akgf2/ Art Business Education
# processed_data_node_id <- "6heqg" # https://osf.io/6heqg/ Housing prices
# processed_data_node_id <- "76tn5" # https://osf.io/76tn5/ Hmm53a-2425-GR1
# processed_data_node_id <- "ab6hg" # https://osf.io/ab6hg/ Hmm53a-2425-GR2
# processed_data_node_id <- "tyhbk" # https://osf.io/tyhbk/ Hmm53a-2425-GR3
osf_retrieve_node(processed_data_node_id) %>%
osf_ls_files(pattern = "Zotero_tag_export_long.csv") %>%
osf_download(path = raw_data_dir, conflicts="overwrite",progress=TRUE)
csv_file <- list.files(path = raw_data_dir, pattern = "\\.csv$", full.names = TRUE)
if (length(csv_file) == 0) {
stop("Error: No csv file found in the specified directory.")
}
# standard pathname
csv_file[1] <- file.path(dirname(csv_file[1]), basename(csv_file[1]))
if (!file.exists(csv_file[1])) {
stop("File does not exist. Check the path.")
}
print(csv_file)
[1] "/var/folders/v0/q9_by2r90yqg0g8s8rykvby40000gn/T//RtmplVKZdG/raw_data/Zotero_tag_export_long.csv"
# Read the Markdown file
Zotero_tag_export_long <- read.csv(csv_file[1], encoding = "UTF-8")
# Check if the file is empty
if (length(Zotero_tag_export_long) == 0) {
stop("Error: The csv file is empty.")
}
own coding
Zotero_tag_export_long <- Zotero_tag_export_long %>%
# Remove backslashes from the 'tag' column
mutate(tag = gsub("\\\\", "", tag)) %>%
# Update the annotation column by combining existing annotation, comment (if applicable), and page info
mutate(annotation = str_squish(
paste(
if_else(is.na(annotation), "", annotation),
if_else(comment != "No Comment", comment, ""),
paste0("(p.", page, ")")
)
)) %>%
# Remove the redundant page and comment columns
select(-page, -comment)
# View the updated dataframe
print(Zotero_tag_export_long)
NA
NA
Zotero_SynthesisTable <- Zotero_tag_export_long %>%
pivot_wider(
id_cols = c(title, author),
names_from = tag,
values_from = annotation,
values_fn = function(x) {
if(length(x) == 1) {
x
} else {
paste0('"', paste(x, collapse = '" "'), '"')
}
}
) %>%
select(title, author, everything())
# View the transformed dataframe
print(Zotero_SynthesisTable)
NA
# Define the full path for the output CSV file
output_file <- file.path(processed_data_dir, "Zotero_SynthesisTable.csv")
write.csv(Zotero_SynthesisTable,output_file,row.names=FALSE)
# Delete the raw_data_dir directory and its contents
# unlink(file.path(tempdir(), "raw_data_dir"), recursive = TRUE)
unlink(raw_data_dir, recursive = TRUE)
# Delete the processed_data_dir directory and its contents
# unlink(file.path(tempdir(), "processed_data_dir"), recursive = TRUE)
unlink(processed_data_dir, recursive = TRUE)